package com.digitalchina.lxnsy.activity.old;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessAccount;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.constant.URL;
import com.digitalchina.lxnsy.entity.CashLimitNewVO;
import com.digitalchina.lxnsy.entity.MyWalletVO;
import com.digitalchina.lxnsy.entity.UserInfo3rdPatyVO;
import com.digitalchina.lxnsy.fragment.WithdrawAliPayFragment;
import com.digitalchina.lxnsy.fragment.WithdrawBankFragment;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.OkHttpUtil;
import com.kekstudio.dachshundtablayout.DachshundTabLayout;
import com.kekstudio.dachshundtablayout.indicators.LineMoveIndicator;
import com.lzy.okhttputils.model.HttpParams;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2018/10/8 15:38
 * 修改备注：
 * ================================================
 */
public class WithdrawMoneyActivity extends BaseActivity {

    @Bind(R.id.tab_layout)
    DachshundTabLayout tabLayout;
    @Bind(R.id.viewPager)
    ViewPager viewPager;


    private String title[] = {"提现到银行卡", "提现到支付宝"};
    private List<Fragment> fragments = new ArrayList<>();
    private MyPagerAdapter adapter;


    WithdrawBankFragment fragment1;
    WithdrawAliPayFragment fragment2;

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_withdraw_money);
        setTitle("提现");
        setRightText("提现记录");
    }

    @Override
    public void initMember() {

        BusinessAccount.get3rdUserInfo(this, mHandler);

        cashLimitNew();

        MyWalletVO myWalletVO = (MyWalletVO) getIntent().getSerializableExtra("myWalletVO");


        tabLayout.setAnimatedIndicator(new LineMoveIndicator(tabLayout));

        fragment1 = new WithdrawBankFragment();
        fragment2 = new WithdrawAliPayFragment();

        Bundle bundle = new Bundle();
        bundle.putString("ActualBal", myWalletVO.getActualBal());
        bundle.putString("CanUseBal", myWalletVO.getCanUseBal());
        bundle.putString("FreezeMoney", myWalletVO.getFreezeMoney());
        fragment1.setArguments(bundle);
        fragment2.setArguments(bundle);

        fragments.add(fragment1);
        fragments.add(fragment2);


        adapter = new MyPagerAdapter(getSupportFragmentManager());
        viewPager.setOffscreenPageLimit(title.length);
        viewPager.setAdapter(adapter);

        viewPager.setOffscreenPageLimit(title.length);

        tabLayout.setupWithViewPager(viewPager);


    }

    @Override
    public void addListener() {
    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                switch (msg.what) {
                    case MSG.MSG_GET_PLAN_ALL_LIST_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_GET_PLAN_LIST_FIELD:
                        break;
                    case MSG_S_0:
                        CashLimitNewVO vo = FastJsonUtil.getBean(msg.obj.toString(),"",CashLimitNewVO.class);
                        fragment1.cashLimitNew(vo);
                        fragment2.cashLimitNew(vo);
                        break;
                    case MSG.MSG_GET_3RD_USER_INFO_SUCCESS: // 绑定列表
                        List<UserInfo3rdPatyVO> list = FastJsonUtil.getListBean(msg.obj.toString(), "userInfo3rdPatyList", UserInfo3rdPatyVO.class);
                        if(list!=null){
                            for (int i = 0; i < list.size(); i++) {
                                if ("WX".equals(list.get(i).getAccountType())){
                                    fragment1.set3rdUserInfo(list.get(i));
                                }else{
                                    fragment2.set3rdUserInfo(list.get(i));
                                }
                            }
                        }
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
    }

    @Override
    public void rightOnClick() {
        GotoUtil.gotoActivity(this,WithdrawHistoryListActivity.class);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    private class MyPagerAdapter extends FragmentStatePagerAdapter {
        private MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return title[position];
        }

    }

    public void cashLimitNew() {
        HttpParams params = new HttpParams();

        OkHttpUtil.post(activity, URL.URL_CASH_LIMIT_NEW, "", params, mHandler,
                MSG_S_0, MSG_F_0);
    }

}
